package com.tm.model;

import com.tm.annotationTest.TmColumnName;
import com.tm.annotationTest.TmTableId;
import com.tm.annotationTest.TmTableName;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;


public class AutoGeneratorTable {

    public  static void  GeneratorTable(Class stuClass){

        try {
            //获取表名，每个字段名，字段的类型，长度
            TmTableName declaredAnnotation = (TmTableName) stuClass.getDeclaredAnnotation(TmTableName.class);
            String stringValue = declaredAnnotation.value();
            //建表的SQL语句
            StringBuffer sql = new StringBuffer("create table "+ stringValue +"(");
            //定义一个
            int index = 0 ;
            Field[] declaredFields = stuClass.getDeclaredFields();
            for (int i = 0; i < declaredFields.length; i++) {
                TmTableId declaredAnnotation1 = declaredFields[i].getDeclaredAnnotation(TmTableId.class);
                if (declaredAnnotation1 != null){
                    sql.append(declaredFields[i].getName());
                    sql.append(" ");
                    sql.append("int(10) NOT NULL AUTO_INCREMENT,");
                    index = i;
                }
                //获取其他的字段
                TmColumnName tmColumnName = declaredFields[i].getDeclaredAnnotation(TmColumnName.class);
                if(tmColumnName != null){
                    sql.append(tmColumnName.value());
                    sql.append(" ");
                    //理论上来说，需要在这里写很多判断，你是什么类型，就设置什么数据库类型
                    sql.append("varchar("+tmColumnName.length()+"),");
                }
            }
            //create table t_teacher_tm (teacherId int(10)primary key,)
            sql.append("PRIMARY KEY (`"+declaredFields[index].getName()+"`)");
            sql.append(")");
            System.out.println(sql);


            Class.forName("com.mysql.cj.jdbc.Driver");

            String url = "jdbc:mysql://localhost:3306/tm4sx?serverTimezone=UTC&useSSL=false";
            //新版的mysql驱动jar包要设置：数据库名，时区，使用ssl等等
            String username = "root";//用户名
            String password = "801404";//密码
            Connection conn = null;  //创建一个引用
            //2.获取与数据库的链接
            conn = DriverManager.getConnection(url, username, password);
            //3.获取用于向数据库发送sql语句的PreparedStatement
            PreparedStatement st = null;
            st = conn.prepareStatement(sql.toString());//在此处传入进行预编译
            st.executeUpdate();//在这里不需要传入sql
             //6.关闭链接，释放资源
            if(st!=null){
                try{
                    st.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try{
                    conn.close();
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
